home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Miscellaneous / Headlines Code / Headlines Project ƒ / HeadlinesMain.c < prev    next >
Text File  |  1992-11-23  |  9KB  |  394 lines

  1. /*
  2.  * HeadlinesMain.c
  3.  *
  4.  * This is © Copyright 1992 by Jamie R. McCarthy.  All rights reserved.
  5.  *
  6.  * Please note that, in Headlines 1.0, I simply repeated Greg Smith's
  7.  * distribution agreement, namely that no monetary gain must be made
  8.  * from this program.  In this version, I'm not distributing his source
  9.  * code, and am changing the terms a little.  Headlines and its source
  10.  * code may not be sold by themselves, but they may be included in any
  11.  * collection of software, as long as that collection, if it contains
  12.  * shareware programs, prominently notifies the purchaser of the
  13.  * additional monetary obligation for those programs.  Apart from that,
  14.  * use and distribute them freely!
  15.  *
  16.  * If you would like to examine Greg's original spew source code, you can
  17.  * probably obtain it from the same place I did:  the wuarchive.wustl.edu
  18.  * ftp archive, in /usenet/comp.sources.games/volume01.
  19.  */
  20.  
  21.  
  22.  
  23. /******************************/
  24.  
  25. #include "HeadlinesMain.h"
  26.  
  27. #include "JMassMalloc.h"
  28. #include "Spyoo.h"
  29. #include "SpyooDisplay.h"
  30.  
  31. /******************************/
  32.  
  33. #define kPStrUnknownError ("\pUnknown error.")
  34.  
  35. #define kInitialPauseTicks (90)
  36. #define kOffScreenSpeedFactor (150)
  37. #define kOnScreenSpeedFactor (600)
  38. #define kMinimumNTicks (6)
  39. #define kSnuggleSpeedFactor (4)
  40. #define kMinNTicksToSnuggle (1)
  41. #define kMaxNTicksToSnuggle (60)
  42. #define kLengthSpeedFactor (9)
  43.  
  44. /******************************/
  45.  
  46.     /* A global for the storage, so I don't have to keep passing it around. */
  47. Handle *gStorage;
  48.  
  49.     /* The input resource (corresponds to "InFile"), its mark, and its length. */
  50. char **gInRsrcHndl;
  51. long gInRsrcMark;
  52. long gInRsrcLength;
  53.  
  54.     /* Is a headline presently being displayed? */
  55. Boolean gHeadlineIsUp;
  56.  
  57.     /* The time at which the last action--display or removal--took place. */
  58. unsigned long gLastActionTicks;
  59.  
  60.     /* The current line being read in. */
  61. char gInLine[256];
  62.  
  63.     /* How many classes there are. */
  64. short gNClasses;
  65.  
  66.     /* A pointer to the array of class records. */
  67. class *gClass;
  68.  
  69.     /* The default tags, which are shared. */
  70. char gNullTags[] = " ";
  71.  
  72.     /* The data for JRandom. */
  73. jrStruct gJR;
  74.  
  75.     /* The headline that will be, is, or was being displayed. */
  76. Str255 gHeadline;
  77.  
  78.     /* Are we working with TrueType? */
  79. Boolean gHasTrueType;
  80.  
  81.     /* Have all the allocations been freed? */
  82. Boolean gHasFreedHeadlinesAllocations = FALSE;
  83.  
  84. /******************************/
  85.  
  86.  
  87.  
  88. OSErr doInitialize(Handle *storage,
  89.     RgnHandle blankRgn,
  90.     short message,
  91.     GMParamBlockPtr params)
  92. {
  93.     long theResponse;
  94.     OSErr theOSErr;
  95.     
  96.     theOSErr = noErr;
  97.     
  98.     gStorage = storage;
  99.     gInRsrcHndl = NULL;
  100.     gInRsrcMark = 0;
  101.     gLastActionTicks = TickCount() + kInitialPauseTicks;
  102.     gHeadlineIsUp = FALSE;
  103.     gFontClass.weight = 0;
  104.     gFontClass.list = NULL;
  105.     gFontClass.name = NULL;
  106.     gFontClass.tags = NULL;
  107.     theOSErr = Gestalt(gestaltFontMgrAttr, &theResponse);
  108.     if (theOSErr != noErr) {
  109.             /* A pre-6.0.4 system;  no TrueType.  Forgive the error. */
  110.         gHasTrueType = FALSE;
  111.         theOSErr = noErr;
  112.     } else {
  113.         gHasTrueType = ( (theResponse & gestaltOutlineFonts) != 0);
  114.     }
  115.     
  116.         /* If storage has already been allocated, something fishy's going on! */
  117.     if (*gStorage != NULL) {
  118.         return storageAlreadyInitedErr;
  119.     }
  120.     
  121.     setupJMM();
  122.     
  123.     IJRandom(&gJR);
  124.     
  125.         /* Load in the resource handle. */
  126.     
  127.     SetResLoad(FALSE);
  128.     gInRsrcHndl = (char**) GetResource('TEXT', 128);
  129.     SetResLoad(TRUE);
  130.     if (gInRsrcHndl == NULL) {
  131.         theOSErr = noHeadlinesErr;
  132.     } else {
  133.         LoadResource(gInRsrcHndl);
  134.         if (*gInRsrcHndl == NULL || ResError() != noErr) {
  135.             theOSErr = memFullErr;
  136.         } else {
  137.             gInRsrcLength = GetHandleSize(gInRsrcHndl);
  138.             HNoPurge(gInRsrcHndl);
  139.         }
  140.     }
  141.     
  142.         /* Make a dummy allocation, in case After Dark gets suspicious. */
  143.     
  144.     if (theOSErr == noErr && *gStorage == NULL) {
  145.         *gStorage = NewHandle( 0L );
  146.         if (*gStorage == NULL) {
  147.             theOSErr = memFullErr;
  148.         }
  149.     }
  150.     
  151.     if (theOSErr == noErr) {
  152.         theOSErr = setupDisplay();
  153.     }
  154.     
  155.     freeHeadlinesAllocationsIfErr(theOSErr);
  156.     
  157.     return theOSErr;
  158. }
  159.  
  160.  
  161.  
  162. OSErr doClose(Handle *storage,
  163.     RgnHandle blankRgn,
  164.     short message,
  165.     GMParamBlockPtr params)
  166. {
  167.     gStorage = storage;
  168.     
  169.     freeHeadlinesAllocations();
  170.     
  171.     shutdownDisplay(params);
  172.     
  173.     return noErr;
  174. }
  175.  
  176.  
  177.  
  178. OSErr doBlank(Handle *storage,
  179.     RgnHandle blankRgn,
  180.     short message,
  181.     GMParamBlockPtr params)
  182. {
  183.     OSErr theOSErr;
  184.     
  185.     theOSErr = noErr;
  186.     gStorage = storage;
  187.     
  188.     PenPat(params->qdGlobalsCopy->qdBlack);
  189.     PenMode(patCopy);
  190.     PaintRgn(blankRgn);
  191.     
  192.     if (theOSErr == noErr && gInRsrcHndl != NULL) {
  193.         HLock(gInRsrcHndl);
  194.         theOSErr = readText();
  195.         HUnlock(gInRsrcHndl);
  196.         
  197.         ReleaseResource( (Handle) gInRsrcHndl);
  198.         gInRsrcHndl = NULL;
  199.     }
  200.     
  201.     freeHeadlinesAllocationsIfErr(theOSErr);
  202.     
  203.     return theOSErr;
  204. }
  205.  
  206.  
  207.  
  208. OSErr doDrawFrame(Handle *storage,
  209.     RgnHandle blankRgn,
  210.     short message,
  211.     GMParamBlockPtr params)
  212. {
  213.     OSErr theOSErr;
  214.     unsigned long nextActionTicks;
  215.     short ticksPastDeadline;
  216.     short speedControlValue;
  217.     
  218.     gStorage = storage;
  219.     theOSErr = noErr;
  220.     
  221.     speedControlValue = 100 - params->controlValues[kSpeedControl];
  222.     nextActionTicks = gLastActionTicks
  223.         + kMinimumNTicks
  224.         + (long)speedControlValue *
  225.             (gHeadlineIsUp ?
  226.                 (kOnScreenSpeedFactor + (long)gHeadline[0]*kLengthSpeedFactor)
  227.                 : (kOffScreenSpeedFactor)
  228.             ) / 100;
  229.     
  230.     ticksPastDeadline = TickCount() - nextActionTicks;
  231.     if (ticksPastDeadline >= 0) {
  232.         
  233.         if (gHeadlineIsUp) {
  234.             theOSErr = remove(params, blankRgn);
  235.             if (theOSErr == noErr) {
  236.                 gHeadlineIsUp = FALSE;
  237.                 gLastActionTicks = TickCount();
  238.             }
  239.         } else {
  240.             
  241.             unsigned short maxNTicksToSnuggle;
  242.             Boolean mustDrawNow;
  243.             
  244.             maxNTicksToSnuggle =
  245.                 speedControlValue * kSnuggleSpeedFactor
  246.                 + kMinNTicksToSnuggle;
  247.             if (maxNTicksToSnuggle > kMaxNTicksToSnuggle) {
  248.                 maxNTicksToSnuggle = kMaxNTicksToSnuggle;
  249.             }
  250.             
  251.             mustDrawNow = (ticksPastDeadline > maxNTicksToSnuggle+4);
  252.             theOSErr = display(params, blankRgn, "MAIN/ ", ' ',
  253.                 maxNTicksToSnuggle, mustDrawNow);
  254.             
  255.             if (theOSErr == noErr) {
  256.                 gHeadlineIsUp = TRUE;
  257.                 gLastActionTicks = TickCount();
  258.             } else if (theOSErr == retryDisplayErr) {
  259.                     /*
  260.                      * Do nothing--try to display it again the next time around.
  261.                      */
  262.                 theOSErr = noErr;
  263.             }
  264.         }
  265.         
  266.     }
  267.     
  268.     freeHeadlinesAllocationsIfErr(theOSErr);
  269.     
  270.     return theOSErr;
  271. }
  272.  
  273.  
  274.  
  275. OSErr doModuleSelected(Handle *storage,
  276.     RgnHandle blankRgn,
  277.     short message,
  278.     GMParamBlockPtr params)
  279. {
  280.     return noErr;
  281. }
  282.  
  283.  
  284.  
  285. OSErr doAbout(Handle *storage,
  286.     RgnHandle blankRgn,
  287.     short message,
  288.     GMParamBlockPtr params)
  289. {
  290.     return noErr;
  291. }
  292.  
  293.  
  294.  
  295. OSErr doButtonMessage(Handle *storage,
  296.     RgnHandle blankRgn,
  297.     short message,
  298.     GMParamBlockPtr params)
  299. {
  300.     return noErr;
  301. }
  302.  
  303.  
  304.  
  305. void setADErrorText(Handle *storage,
  306.     RgnHandle blankRgn,
  307.     short message,
  308.     GMParamBlockPtr params,
  309.     OSErr theOSErr)
  310. {
  311.     adErrPStrCpy(params, "\pHeadlines: ");
  312.     
  313.     switch (theOSErr) {
  314.         
  315.         case noErr:
  316.             break;
  317.         case ModuleError:
  318.             adErrPStrCat(params, "\pModule error.");
  319.             break;
  320.         case memFullErr:
  321.             adErrPStrCat(params, "\pOut of memory.");
  322.             break;
  323.         case expectedClassDefnErr:
  324.             adErrPStrCat(params, "\pExpected a class definition.");
  325.             break;
  326.         case expectedClassInstanceErr:
  327.             adErrPStrCat(params, "\pExpected a class instance.");
  328.             break;
  329.         case tooManyClassesErr:
  330.             adErrPStrCat(params, "\pToo many classes.");
  331.             break;
  332.         case badClassHeaderErr:
  333.             adErrPStrCat(params, "\pA bad class header was found.");
  334.             break;
  335.         case badLineWeightErr:
  336.             adErrPStrCat(params, "\pA bad line weight was found.");
  337.             break;
  338.         case noHeadlinesErr:
  339.             adErrPStrCat(params, "\pNo headlines were found.");
  340.             break;
  341.         case noMainClassErr:
  342.             adErrPStrCat(params, "\pNo class named MAIN was found.");
  343.             break;
  344.         case storageAlreadyInitedErr:
  345.             adErrPStrCat(params, "\pStorage was already initialized.");
  346.             break;
  347.         case noFontInfoErr:
  348.             adErrPStrCat(params, "\pNo font info resource was found.");
  349.             break;
  350.         case badFontInfoErr:
  351.             adErrPStrCat(params, "\pThe font info resource is bad.");
  352.             break;
  353.         case unknownErr:
  354.         default: {
  355.             Str15 theValString;
  356.             adErrPStrCat(params, "\pUnknown error, #");
  357.             NumToString(theOSErr, theValString);
  358.             adErrPStrCat(params, theValString);
  359.             adErrPStrCat(params, "\p.");
  360.         }    break;
  361.         
  362.     }
  363. }
  364.  
  365.  
  366.  
  367. void freeHeadlinesAllocations(void)
  368. {
  369.     if (gHasFreedHeadlinesAllocations) return;
  370.     
  371.     gHasFreedHeadlinesAllocations = TRUE;
  372.     
  373.     if (*gStorage != NULL) {
  374.         DisposHandle(*gStorage);
  375.         *gStorage = NULL;
  376.     }
  377.     
  378.     if (gInRsrcHndl != NULL) {
  379.         ReleaseResource( (Handle) gInRsrcHndl);
  380.         gInRsrcHndl = NULL;
  381.     }
  382.     
  383.     shutdownJMM();
  384. }
  385.  
  386.  
  387.  
  388. void freeHeadlinesAllocationsIfErr(OSErr theOSErr)
  389. {
  390.     if (theOSErr != noErr) freeHeadlinesAllocations();
  391. }
  392.  
  393.  
  394.